#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
@software: PyCharm
@time: 2022/9/3 13:03
"""
import os
import yaml


class conf(object):
    pass


class Configuration:
    config = conf()

    def __init__(self, conf_file="config.yaml"):
        self._file = conf_file
        self.conf_dict = dict()
        if not os.path.exists(self._file):
            raise FileNotFoundError("配置文件未找到！")
        else:
            self.read()

    def read(self):
        with open(self._file, 'r', encoding='utf8') as f:
            self.conf_dict = yaml.load(f, yaml.FullLoader)

    @classmethod
    def get_config(cls, conf_file="config.yaml"):
        conf_obj = cls(conf_file)
        for key, value in conf_obj.conf_dict.items():
            convert_obj(cls.config, key, value)
        return cls.config


def convert_obj(obj, key, value):
    if isinstance(value, list):
        setattr(obj, key, [])
        for item in value:
            if not isinstance(item, dict) and not isinstance(item, list):
                getattr(obj, key).append(item)
            elif isinstance(item, dict):
                item_obj = conf()
                getattr(obj, key).append(item_obj)
                for sub_key, sub_value in item.items():
                    convert_obj(item_obj, sub_key, sub_value)

    elif not isinstance(value, dict):
        setattr(obj, key, value)
    else:
        setattr(obj, key, conf())
        for sub_key, sub_value in value.items():
            convert_obj(getattr(obj, key), sub_key, sub_value)
    return obj


configuration = Configuration.get_config()
print(configuration.log.maxBytes)
